var title;
var api = "/api/news/list";
$(function () {
    title = $("input[name='title']").val();
    console.log(title)
    $('title').text('广联官方网站 新闻活动 ' + title);
    $(".product-nav-title span").text(title);
    $(".tag-item").text(title);
    if (title === "企业资讯") {
        $(".banner-wrap img").attr("src", "http://localhost:8080/upload/img-WvMY24-336441.jpg");
        $(".introduction-category-item a").eq(2).addClass('bottom-line');
        $(".introduce-tag-title").text("企业资讯");
    } else if (title === "展会活动") {
        $(".banner-wrap img").attr("src", "http://localhost:8080/upload/img-OsBjNE-998444.jpg");
        $(".introduction-category-item a").eq(3).addClass('bottom-line');
        $(".introduce-tag-title").text("展会活动");
    }

    var data = {
        "category_name": title,
    }
    newsActivitiesPagination(data, 1);
})
$(".introduction-category-item a").click(function () {
    $(".product-nav-title").text($(this).text())
    if ($(this).text() === '新闻活动') {

    }else if($(this).text() === title){

    }else {
        $(this).addClass("bottom-line").parent().siblings().children().removeClass("bottom-line");
    }

})

//enter键盘事件
$('.search').keydown(function (e) {
    if (e.keyCode === 13) {
        var name = $(".search").val();
        console.log(name)
        var data = {
            "category_name":title,
            "title": name,
        }
        newsActivitiesPagination(data,1 ,name)
    }
});

//点击搜索按钮
$('.searchBut').on('click', function (e) {
    var name = $(".search").val();
    var data = {
        "category_name":title,
        "title": name,
    }
    newsActivitiesPagination(data,1 ,name)
})


function newsActivitiesRenderTable(data) {
    var html = "";
    $(".smartEnergy-main").empty();
    $.each(data, function (index, item) {
        var curTime = new Date(item.modify_date).format("yyyy-MM-dd");
        html = $('<a href="/news/socialResponsibility?id=' + item.id + '" class="smartEnergy-item">\n' +
            '                    <div class="smartEnergy-item-img">\n' +
            '                        <img class="smartEnergy-img" src="' + item.img + '">\n' +
            '                    </div>\n' +
            '                    <div class="smartEnergy-title-item">\n' +
            '                        <div class="smartEnergy-title">' + item.title + '</div>\n' +
            '                        <div class="smartEnergy-date">' + curTime + '</div>\n' +
            '                        <div class="smartEnergy-but">\n' +
            '                            了解更多\n' +
            '                        </div>\n' +
            '                    </div>\n' +
            '                </a>')
        $(".smartEnergy-main").append(html);

    })
}

function newsActivitiesPagination(data, currentPage, titleName) {
    postJSON(api, data, function (res) {
        if (res.data.length > 0) {
            $(".pagination-warp").show();
            $(".count-tag span").text(res.count);
            $.jqPaginator("#pagination1", {
                totalPages: Math.ceil(res.count / 6),
                visiblePages: 6,
                currentPage: currentPage,
                first: '<li class="first"><a href="javascript:void(0);">首页</a></li>',
                prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
                next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
                last: '<li class="last"><a href="javascript:void(0);">末页</a></li>',
                page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
                onPageChange: function (page) {
                    // data.offset = page;
                    console.log(page);
                    var data = {
                        "page": page,
                        "category_name": title,
                        "title": titleName,
                    }
                    postJSON(api, data, function (res) {
                        newsActivitiesRenderTable(res.data);
                    })
                }
            })
        } else {
            $(".smartEnergy-main").empty();
            $(".pagination-warp").hide();
            $(".smartEnergy-main").html("<div class=\"product-tag-item\">暂时还没有活动</div>");
        }
    })

}